﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace TemplateTool.Encrypt
{
    public class EncryptCalc
    {
        /// <summary>
        /// 用MD5加密字符串
        /// </summary>
        /// <param name="password">待加密的字符串</param>
        /// <returns></returns>
        public static string MD5Encrypt(string password)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] hashedDataBytes;
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("GB2312").GetBytes(password));
            StringBuilder tmp = new StringBuilder();
            foreach (byte i in hashedDataBytes)
            {
                tmp.Append(i.ToString("x2"));
            }
            return tmp.ToString();
        }

        //获取盐（用户登录密码）+生成验证码文字
        public static string CreateCode(int len)
        {
            char[] data = { 'a','c','b','d','e','2','f','h','k','m',
                'n','8','r','s','5','6','7','4','t','3','w','x','y','z','1'};
            StringBuilder sb = new StringBuilder();
            Random rand = new Random();
            for (int i = 0; i < len; i++)
            {
                int index = rand.Next(data.Length);//[0,data.length)
                char ch = data[index];
                sb.Append(ch);
            }
            return sb.ToString();
        }
    }
}
